Imports System.Data.SqlClient
Imports System.Configuration

''' <summary>
''' DSN Manager.
''' </summary>
''' <remarks>
''' Using the application.config or web.config - 
''' value of Pixolut.BizBlox.ConnectionString contains a valid connection string.
''' value of Pixolut.BizBlox.EncryptedConnectionString contains a valid MD5 encrypted connection string.
''' value of Pixolut.BizBlox.EncryptedConnectionString.1 to .9 contains a valid MD5 encrypted connection string which is TESTED.
''' 
''' </remarks>
''' <history>
''' 	[cinj]	1/09/2004	Created
''' </history>
Public Class DSNManager
    'DSN Management members
    Private Const DSN_KEY As String = "Pixolut.BizBlox.ConnectionString"
    Private Const XDSN_KEY As String = "Pixolut.BizBlox.EncryptedConnectionString"
    Private Const SKEY As String = "Pixolut.BizBlox.ActiveConnection"
    Private Shared m_cached As Boolean = False
    Private Shared m_DSNKey As String
    Private Shared m_xDSNKey As String
    Private Shared m_DSNOverride As Boolean = False
    Private Shared m_xDSNOverride As Boolean = False
    Private Shared m_connStr As String

    'use the setdsn in a derived object constructor to change the 
    'DSN key used in the WEBINFO. The default is "DataObject"

    ''' <summary>
    ''' Override the connection string specified in the web.config 
    ''' </summary>
    ''' <param name="dsn">The connection string</param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[cinj]	1/09/2004	Created
    ''' </history>

    Public Shared Sub OverrideDSNKey(ByVal dsn As String)
        m_DSNKey = dsn
        m_DSNOverride = True
    End Sub

    Public Shared Sub OverrideEncryptedDSNKey(ByVal dsn As String)
        m_xDSNKey = dsn
        m_DSNOverride = True
    End Sub

    Public Shared Sub OverrideConnectionString(ByVal connStr As String)
        SyncLock DSNManager.m_connStr
            m_connStr = connStr
            m_cached = True
        End SyncLock
    End Sub


    ''' <summary>
    ''' Get the currently specified connection string
    ''' </summary>
    ''' <returns>connection string</returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[cinj]	1/09/2004	Created
    ''' </history>

    Public Shared Function GetConnectionString() As String
        If m_cached Then Return m_connStr

        Try
            Dim srcID As String = ConfigurationManager.AppSettings(SKEY)
            Dim srcBuf As String = ConfigurationManager.AppSettings(XDSN_KEY & "." & srcID)
            If Not srcBuf Is Nothing Then
                m_connStr = DecryptConnectionString(srcBuf)
                m_cached = True
                Return m_connStr
            End If
        Catch ex As Exception
        End Try

        Try
            Dim srcBuf As String = ConfigurationManager.AppSettings(IIf(m_xDSNOverride = False, XDSN_KEY, m_xDSNKey))
            If Not srcBuf Is Nothing Then
                m_connStr = DecryptConnectionString(srcBuf)
                m_cached = True
                Return m_connStr
            End If
        Catch ex As Exception
        End Try

        Try
            m_connStr = ConfigurationManager.AppSettings(IIf(m_DSNOverride = False, DSN_KEY, m_DSNKey))

            If Not m_connStr Is Nothing Then
                m_cached = True
                Return m_connStr
            End If
        Catch ex As Exception
        End Try

        m_cached = False
        Return Nothing
    End Function




    ''' <summary>
    ''' Encrypt a connection string
    ''' </summary>
    ''' <param name="srcConnStr">the connection string to be encrypted.</param>
    ''' <param name="id">the system hardware id [accessible for testing purposes].</param>
    ''' <returns></returns>
    ''' <remarks>
    ''' uses the MD5 crypt.
    ''' </remarks>
    ''' <history>
    ''' 	[cinj]	7/09/2004	Created
    ''' </history>

    Public Shared Function EncryptConnectionString(ByVal srcConnStr As String, ByVal id As String) As String
        Dim x As New SimpleCrypt
        x.Key = id
        x.OriginalString = srcConnStr
        Return x.EncryptedString
    End Function


    ''' <summary>
    ''' Encrypt a connection string
    ''' </summary>
    ''' <param name="srcConnStr">the connection string to be encrypted.</param>
    ''' <returns></returns>
    ''' <remarks>
    ''' uses the MD5 crypt.
    ''' </remarks>
    ''' <history>
    ''' 	[cinj]	7/09/2004	Created
    ''' </history>

    Public Shared Function DecryptConnectionString(ByVal srcConnStr As String) As String
        Dim x As New SimpleCrypt(srcConnStr, HardwareID.SystemHash)
        Return x.OriginalString
    End Function

    Public Shared Function DecryptConnectionString(ByVal srcConnStr As String, ByVal hardwareCode As String) As String
        Dim x As New SimpleCrypt(srcConnStr, hardwareCode)
        Return x.OriginalString
    End Function


End Class
